﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Presentation.Module.Security.Interfaces.ViewModels;
using System.Windows.Input;
using Presentation.Module.Security.Model;
using Microsoft.Practices.Prism.Commands;
using Presentation.Module.Security.Interfaces.Services;
using Presentation.Module.Security.Interfaces.Views;
using System.ComponentModel;
using Microsoft.Practices.Prism.Regions;

namespace Presentation.Module.Security.ViewModels
{
    public class UserViewModel : UserViewModelBase, IUserViewModel, INotifyPropertyChanged
    {
        private User currentUser;

        private IUserService _userService;        

        private IRegionManager _regionManager;

        private IAddUserView _addUserView;

        public ICommand AddUser { get; private set; }

        public ICommand EditUser { get; private set; }

        public ICommand DeleteUser { get; private set; }

        public UserViewModel(IRegionManager regionManager, IUserService userService, IAddUserView addUserView)
            : base(regionManager, userService)
        {
            this.AddUser = new DelegateCommand(this.OnAddUser);
            this.EditUser = new DelegateCommand<User>(this.OnEditUSer);
            this.DeleteUser = new DelegateCommand<User>(this.OnDeleteUser);

            this._regionManager = regionManager;
            this._userService = userService;
            this._addUserView = addUserView;
            this._regionManager.Regions["MainRegion"].Add(addUserView);

            this._addUserView.ViewExit += this.ChildViewExit;

            currentUser = new User();
        }

        public User CurrentUser 
        { 
            get
            {
                return this.currentUser;
            } 
            
            set
            {
                if (value == null)
                {
                    return;
                }

                this.currentUser = value;
                this.RaiseOnPropertyChanged("Users");
            } 
        }

        public ObservableCollection<User> Users
        {
            get { return this._userService.GetAllUsers(); } 
        }               

        public void OnAddUser()
        {
            _regionManager.Regions["MainRegion"].Activate(this._addUserView);
        }

        public void OnEditUSer(User user)
        {
            this._userService.EditUser(user);
        }

        public void OnDeleteUser(User user)
        {
            this._userService.DeleteUser(user);
            this.RaiseOnPropertyChanged("Users");
        }

        private void ChildViewExit()
        {
            this._regionManager.Regions["MainRegion"].Activate(this.View);
            this.RaiseOnPropertyChanged("Users");
        }

        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public IUserView View { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
